﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScreenMonitor.Common
{
    /// <summary>
    /// 为可通过内存流发送的对象提供集合包装
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MemorySendableList<T> : MemoryStreamSendableObjectBase<MemorySendableList<T>>,
         IList<T>,IReadOnlyList<T>, IReadOnlyCollection<T>,
        ISendableObject 
        where T:IStreamSendableObject,new()
    {

        private List<T> _root=new List<T>();

        #region========基类实现========

        /// <summary>
        /// 将当前集合写入到基础流中
        /// </summary>
        /// <param name="sw">负责写入的基础流写入器</param>
        public override void ToStream(System.IO.BinaryWriter sw)
        {
            sw.Write(this.Count);
            foreach(T item in this)
            {
                item.ToStream(sw);
            }
        }

        /// <summary>
        /// 从指定读取器读取数据到集合中（此操作会清空集合）
        /// </summary>
        /// <param name="sr"></param>
        public override void FromStream(System.IO.BinaryReader sr)
        {
            _root.Clear();
            AddFromStream(sr);
        }

        /// <summary>
        /// 从指定读取器读取数据到集合中（不会清空集合）
        /// </summary>
        /// <param name="sr"></param>
        public void AddFromStream(System.IO.BinaryReader sr)
        {
            int ItemC = sr.ReadInt32();
            for(int i=0;i<ItemC;i++)
            {
                T item = new T();
                item.FromStream(sr);
                this.Add(item);
            }
        }

        #endregion

        #region========集合公开========

        public int IndexOf(T item)
        {
            return _root.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            _root.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _root.RemoveAt(index);
        }

        public T this[int index]
        {
            get
            {
                return _root[index];
            }
            set
            {
                _root[index] = value;
            }
        }

        public void Add(T item)
        {
            _root.Add(item);
        }

        public void Clear()
        {
            _root.Clear();
        }

        public bool Contains(T item)
        {
            return _root.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _root.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _root.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            return _root.Remove(item);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _root.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _root.GetEnumerator();
        }

        #endregion

       
    }
}
